import 'package:flutter/material.dart';
import 'package:flutter_navigator/Core/OSState.dart';
import 'package:flutter_navigator/OSNavigator/OSNavigator.dart';
import 'package:provider/provider.dart';

import 'Model/VideoModel.dart';

/// 状态管理类 之 Provider
/// 四步骤上手Provider
/// 1.创建自己需要共享的数据
/// 2.在程序的顶层创建ChangeNotifierProvider
/// 3.在其他位置共享数据
/// 4.Consumer 修改数据通知更新

//1.创建自己需要共享的数据
class AMCommonViewModel with ChangeNotifier {
  int _count = 10;
  int get count => _count;
  set count(int value) {
    _count = value;
    // 通知监听者，更新数据
    notifyListeners();
  }
}

class AMHomePage extends StatefulWidget {
  const AMHomePage({Key? key}) : super(key: key);
  @override
  _AMHomePageState createState() => _AMHomePageState();
}

class _AMHomePageState extends OSState<AMHomePage> {
  var listener;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    OSNavigator.getInstance().addListener(this.listener = (current, pre) {
      print("当前页面：${current.page}");
      print("上一个页面：${pre.page}");
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    OSNavigator.getInstance().removeListener(this.listener);
    super.dispose();
  }

  // 变量声明在 build方法外面，如果在build内部声明，重新build每次都会声明，相当于变量永远是新的，值永远是初始化的值
  Widget build(BuildContext context) {
    /// 2.在程序的顶层创建ChangeNotifierProvider(我这里仅限HomePage,如果需要最顶层，可以包裹runApp)
    return ChangeNotifierProvider(
      create: (ctx) => AMCommonViewModel(),
      child: Scaffold(
        appBar: AppBar(
          title: Text("逻辑资源"),
        ),
        body: Column(
          children: [
            Container(
              height: 110,
              width: double.infinity,
              child: Card(
                color: Colors.orange,
                child: Column(
                  children: [
                    MaterialButton(
                      color: Colors.redAccent,
                      textColor: Colors.white,
                      onPressed: () {
                        OSNavigator.getInstance().onJumpTo(
                            RouteStatus.homeDetail,
                            args: {"videoModel": VideoModel(180)});
                      },
                      child: Text("点击跳转详情"),
                    ),
                    Selector<AMCommonViewModel, AMCommonViewModel>(
                      selector: (ctx, countVM) => countVM,
                      shouldRebuild: (prev, next) => false,
                      //优化child不重建
                      child: Text("计数器+1"),
                      builder: (ctx, countVM, child) {
                        return MaterialButton(
                          color: Colors.redAccent,
                          textColor: Colors.white,
                          onPressed: () {
                            /// 修改数据
                            setState(() {
                              countVM.count += 1;
                            });
                          },
                          child: child,
                        );
                      },
                    ),
                  ],
                ),
              ),
            ),
            AMCardView01(),
            AMCardView02()
          ],
        ),
      ),
    );
  }
}

class AMCardView01 extends StatelessWidget {
  const AMCardView01({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ///3.在其他位置共享数据
    var resCount = Provider.of<AMCommonViewModel>(context).count;
    return Container(
      height: 80,
      width: double.infinity,
      child: Card(
        color: Colors.orange,
        child: Column(
          children: [
            Center(
              child: Text("当前的数量为$resCount"),
            )
          ],
        ),
      ),
    );
  }
}

class AMCardView02 extends StatelessWidget {
  ///3.在其他位置共享数据
  const AMCardView02({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var resCount = Provider.of<AMCommonViewModel>(context).count;
    return Container(
      height: 80,
      width: double.infinity,
      child: Card(
        color: Colors.yellow,
        child: Column(
          children: [
            Center(
              child: Text("当前的数量为$resCount"),
            )
          ],
        ),
      ),
    );
  }
}
